package com.foreveross.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.OrderInfoDAO;
import com.foreveross.util.DateUtil;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrBusiness;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.HrFavorable;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.OrderInfo;
import com.foreveross.vo.PayCostFavorableInfo;
import com.foreveross.vo.Tenement;
import com.foreveross.vo.condition.Business;
import com.foreveross.vo.condition.Favorable;
import com.foreveross.vo.condition.FavorableCondition;

@Service("orderInfoService")
@Transactional
public class OrderInfoService extends BaseService{

	@Resource
	OrderInfoDAO orderInfoDAO;
	@Resource
	private HouseUserService houseUserService;
	@Resource
	private HrFavorableService hrFavorableService;
	@Resource
	private TenementService tenementService;
	@Resource
	private PayCostFavorableInfoService payCostFavorableInfoService;
	
	
	public long getOrderInfoListCount(String weixinId,String userId,String houseId,String tenementId,String businessName,
			String payType,String payStatus,String startOrderTime,String endOrderTime,String resCode) throws Exception{
		long orderInfoListCount = 0;
		if(weixinId!=null && !"".equals(weixinId)){
			orderInfoListCount = orderInfoDAO.findOrderInfoListCount(weixinId,userId,houseId,tenementId,businessName,null,
					payType,payStatus,startOrderTime,endOrderTime,resCode);
		}else{
			orderInfoListCount = orderInfoDAO.findOrderInfoListCountWithOutManager(weixinId,userId,houseId,tenementId,businessName,null,
					payType,payStatus,startOrderTime,endOrderTime,resCode);
		}
		return orderInfoListCount;
	}

	public List<OrderInfo> getOrderInfoList(String weixinId,String userId,String houseId,String tenementId,String businessName,
			String payType,String payStatus,String startOrderTime,String endOrderTime,String resCode,int pageIndex,
			int pageSize) throws Exception {
		List<OrderInfo> list = null;
		if(weixinId!=null && !"".equals(weixinId)){
			list = orderInfoDAO.findOrderInfoList(weixinId,userId,houseId,tenementId,businessName,null,
					payType,payStatus,startOrderTime,endOrderTime,resCode,pageIndex,pageSize);
		}else{
			list = orderInfoDAO.findOrderInfoListWithOutManager(weixinId,userId,houseId,tenementId,businessName,null,
					payType,payStatus,startOrderTime,endOrderTime,resCode,pageIndex,pageSize);
		}
		return list;
	}
	
	public OrderInfo getOrderInfoById(String id) throws Exception  {
		return (OrderInfo) orderInfoDAO.findById(OrderInfo.class, id);
	}

	public void saveOrUpdate(OrderInfo OrderInfo, String updateType) throws Exception {
		if (updateType.equals(Constants.UPDATE_ADD)) {
			orderInfoDAO.save(OrderInfo);
		} else if (updateType.equals(Constants.UPDATE_EDIT)) {
			orderInfoDAO.updateObject(OrderInfo);
		}
	}
	
	public void deleteOrderInfo(String ids) throws Exception{
		if(ids == null) return ;
		for(String id :ids.split(",")){
			orderInfoDAO.deleteOrderInfo(id);
		}
	}
	
	public Map<String,Object> saveOrderInfo(String weixinId,FavorableCondition favorableCondition) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		List<String> ids = new ArrayList<String>();
		Tenement tenement = null;
		for(Business business :favorableCondition.getBusiness()){
			HrBusiness hrBusiness = (HrBusiness)orderInfoDAO.findById(HrBusiness.class, business.getBusinessId());
			List<Tenement> tenementList = tenementService.findTenementList(hrBusiness.getHouseId().getId(), null, null, 0, Constants.PAGE_MAX_SIZE, null);
			if(tenementList != null && tenementList.size()>0) {
				tenement = tenementList.get(0);
				map.put("appId", tenement.getAppId());
				map.put("appSecret", tenement.getAppSecret());
				map.put("mchId", tenement.getMchId());
				map.put("apiKey", tenement.getApiKey());
				map.put("certUrl", tenement.getCertUrl());
			}
			
			for(Favorable favorable :business.getFavorable()){
				OrderInfo orderInfo = new OrderInfo();
				orderInfo.setCount(favorable.getCount());
				orderInfo.setHrHouses(hrBusiness.getHouseId());
				orderInfo.setHrBusiness(hrBusiness);
				//HrFavorable hrFavorable = hrFavorableService.getFavorableById(favorable.getFavorableId());
				orderInfo.setHrFavorable(new HrFavorable(favorable.getFavorableId()));
				orderInfo.setOrderTime(new Date());
				orderInfo.setPayStatus(Constants.PAY_STATUS_FAIL);
				orderInfo.setPayType(Constants.PAY_TYPE_ONLINE);
				orderInfo.setTenement(tenement);
				orderInfo.setHouseUser(new HouseUser(favorableCondition.getHouseUserId()));
				//orderInfo.setHouseUser(houseUserService.getHouseUserByweixinId(weixinId));
				
				orderInfoDAO.save(orderInfo);
				ids.add(orderInfo.getId());
			}
		}
		map.put("orderIds", ids);
			
		return map;
	}
	
	
	public List<OrderInfo> updateOrderInfo(String orderIds,int payStatus,Date chargeTime,
			String payType,String outTradeNo) throws Exception{
		List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
		if(orderIds!=null && !"".equals(orderIds)){
			for(String id :orderIds.split(",")){
				OrderInfo orderInfo = this.getOrderInfoById(id);
				if(!"".equals(payStatus)){
					orderInfo.setPayStatus(payStatus);
				}
				if(chargeTime!=null && !"".equals(chargeTime)){
					orderInfo.setChargeTime(chargeTime);
				}
				if(payType!=null && !"".equals(payType)){
					orderInfo.setPayType(Integer.parseInt(payType));
				}
				if(outTradeNo!=null && !"".equals(outTradeNo)){
					orderInfo.setOutTradeNo(outTradeNo);
				}
				OrderInfo o = orderInfoDAO.updateObject(orderInfo);
				orderInfoList.add(o);
			}
		}
		return orderInfoList;
	}
	
	public List<OrderInfo> offLineOrderInfo(String orderIds ,int payStatus) throws Exception{
		//更新订单状态
		List<OrderInfo> orderInfoList = payCostFavorableInfoService.offLineOrderInfoCallBack(orderIds, payStatus);
		
		//更新产品销售数量
		for(OrderInfo orderInfo : orderInfoList){
			hrFavorableService.setCountForHrFavorable(orderInfo.getHrFavorable().getId());
		}
		
		return orderInfoList;
	}
	
	
	/**
	 * 退款处理
	 * @param orderIds
	 * @return
	 * @throws Exception
	 */
	public List<OrderInfo> refundOrderInfoAndPayCostFavorableInfo(String orderIds) throws Exception{
		List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
		if(orderIds!=null && !"".equals(orderIds)){
			for(String id :orderIds.split(",")){
				OrderInfo orderInfo = this.getOrderInfoById(id);
				orderInfo.setPayStatus(Constants.PAY_STATUS_REFUND_SUCCESS);

				OrderInfo o = orderInfoDAO.updateObject(orderInfo);
				orderInfoList.add(o);
				
				List<PayCostFavorableInfo> payCostFavorableInfoList = payCostFavorableInfoService.getPayCostFavorableInfoByOrderId(id);
				for(PayCostFavorableInfo payCostFavorableInfo : payCostFavorableInfoList){
					payCostFavorableInfo.setPayStatus(Constants.PAY_STATUS_REFUND_SUCCESS);
					payCostFavorableInfoService.updateObject(payCostFavorableInfo);
				}
				
				hrFavorableService.setCountForHrFavorable(orderInfo.getHrFavorable().getId());
			}
		}
		return orderInfoList;
	}
	
}
